<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\pull;


use app\common\model\pull\ChannelQuickapp;
use app\common\model\pull\Pull;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * Pull逻辑
 * Class PullLogic
 * @package app\adminapi\logic
 */
class PullLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/01/19 16:05
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            Pull::create([
                'name' => $params['name'],
                'online' => $params['online'],
                'booklist_url' => $params['booklist_url'],
                'bookinfo_url' => $params['bookinfo_url'],
                'chapterlist_url' => $params['chapterlist_url'],
                'chaptercontent_url' => $params['chaptercontent_url'],
                'app_key' => $params['app_key'],
                'app_secret' => $params['app_secret'],
                'merchant_id' => $params['merchant_id'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/01/19 16:05
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            Pull::where('id', $params['id'])->update([
                'name' => $params['name'],
                'booklist_url' => $params['booklist_url'],
                'online' => $params['online'],
                'bookinfo_url' => $params['bookinfo_url'],
                'chapterlist_url' => $params['chapterlist_url'],
                'chaptercontent_url' => $params['chaptercontent_url'],
                'app_key' => $params['app_key'],
                'app_secret' => $params['app_secret'],
                'merchant_id' => $params['merchant_id'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }
    /**
     * @notes 配置快应用
     * @param $adminId
     * @param $teamIds
     * @throws \Exception
     * @author 段誉
     * @date 2022/11/25 14:22
     */
    public static function sitePlatform($params)
    {
        // 快应用
        $platforms = explode(',',$params['platforms']);
        if (!empty($platforms)) {
            $platformsData = [];
            foreach ($platforms as $platform) {
                $platformsData[] = [
                    'channel_id' => $params['id'],
                    'platform' => $platform
                ];
            }
            ChannelQuickapp::where('channel_id',$params['id'])->delete();
            (new ChannelQuickapp())->saveAll($platformsData);
            return true;
        }else{
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/01/19 16:05
     */
    public static function delete(array $params): bool
    {
        return Pull::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/01/19 16:05
     */
    public static function detail($params): array
    {
        return Pull::findOrEmpty($params['id'])->toArray();
    }

    public static function getAll(): array
    {
        return Pull::where('online',1)->field('id,name')->order('id asc')->select()->toArray();
    }
}